package com.asp.aspchain.mmwyaccount.service;

import com.asp.aspchain.mmwyaccount.constants.ErrorCode;
import com.asp.aspchain.mmwyaccount.request.CrtAccountReq;
import com.alibaba.fastjson.JSON;
import com.asp.aspchain.core.dao.AppBaseMapper;
import com.asp.aspchain.core.dao.TocAccountMapper;
import com.asp.aspchain.core.ethereum.EthService;
import com.asp.aspchain.core.model.AppBaseModel;
import com.asp.aspchain.core.model.TocAccountModel;
import com.asp.aspchain.core.service.ISequenceService;
import com.asp.aspchain.core.util.AjaxResult;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Date;

@Service
public class MmwyCrtaccService {
	private final static Logger logger = Logger.getLogger(MmwyCrtaccService.class);

	@Autowired
	private TocAccountMapper tocAccountMapper;
	@Autowired
	EthService ethService;
	@Autowired
	AppBaseMapper appBaseMapper;
	@Autowired
	ISequenceService sequenceService;

	//账户类型：coa账户
	private String formtype = "01";
	//token类型：eth
	private String tokentype = "MMWY";
	//状态：锁定
	private String status = "01";

	public AjaxResult mmwyCrtaccount(String json) {
		CrtAccountReq accountReq = JSON.parseObject(json, CrtAccountReq.class);
		//获取app参数
		AppBaseModel appBaseModel = new AppBaseModel();
		appBaseModel.setAppmark(accountReq.getAppmark());
		appBaseModel = appBaseMapper.selectOne(appBaseModel);

		Integer isdiyaccount = appBaseModel.getIsdiyaccount();
		String accountid = null;
		//isdiyaccount参数等于0则用以太坊的账户
		if (isdiyaccount.intValue() == 0) {
			//如果密码可为空
			try {
				accountid = ethService.newAccount(accountReq.getPassword());
			} catch (IOException e) {
				logger.info("mmwy创建账户时发生错误");
				e.printStackTrace();
				return new AjaxResult(ErrorCode.CREATE_FAIL, "mmwy创建账户失败", "mmwy创建账户失败");
			}
		} else {
			//否则自定义账户，10000开始自增，转成16进制字符串
			String id = sequenceService.getNextStringValue("accountid");
			String HexId = Integer.toHexString(Integer.parseInt(id));
			String zero = "";
			for (int i = 0; i < 40 - HexId.length(); i++) {
				zero = zero + "0";
			}
			accountid = "0x" + zero + HexId;
		}
		if (StringUtils.isBlank(accountid)) {
			return new AjaxResult(ErrorCode.CREATE_FAIL, "mmwy创建账户失败", "mmwy创建账户失败");
		}

		//入库
		TocAccountModel tocAccountModel = new TocAccountModel();
		tocAccountModel.setAccountid(accountid);
		tocAccountModel.setAppid(appBaseModel.getAppid());
		tocAccountModel.setCreatetime(new Date());
		tocAccountModel.setUpdatetime(tocAccountModel.getCreatetime());
		tocAccountModel.setFromtype(formtype);
		tocAccountModel.setTokentype(tokentype);
		tocAccountModel.setIdtype(accountReq.getIdtype());
		tocAccountModel.setIdentityId(accountReq.getIdentification());
		tocAccountModel.setPhone(accountReq.getPhonenum());
		tocAccountModel.setStatus(status);
		tocAccountModel.setPassword(accountReq.getPassword());
		tocAccountMapper.insert(tocAccountModel);

		return new AjaxResult(ErrorCode.SUCCESS, "mmwy创建账户成功", accountid, "mmwy创建账户成功");
	}

	public static void main(String[] args) {
		String HexId = Integer.toHexString(Integer.parseInt("100"));
		String zero = "";
		for (int i = 0; i < 40 - HexId.length(); i++) {
			zero = zero + "0";
		}
		String accountid = "0x" + zero + HexId;
	}
}
